import { ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
export default function (apiFn) {
  let data = ref([])
  const target = ref(null)
  const { stop } = useIntersectionObserver(
    target,
    ([{ isIntersecting }], observerElement) => {
      if (isIntersecting) {
        stop()
        apiFn().then(({ code, result }) => {
          if (code == 1) {
            data.value = result
          }
        })
      }
    },
    // 配置选项，相交的比例大于0就触发
    {
      threshold: 0
    }
  )

  return { result: data, target }
}
